﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

namespace LabMS.Web.Equipment.common
{
    public partial class recipientappperview : Base.BasePage
    {
        public string strID = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            strPagePrivilege.Add("JCSQSH");//必须写在ispostback外
            strPageUserType.Add("mana");
            strPageUserType.Add("teacher");

            if (Request.QueryString["id"] == null)
            {
                Common.JShelper.JSAlert(Page, "err", "参数错误!");
                btnApp.Visible = false;
                btnDel.Visible = false;
                btnEdit.Visible = false;
                btnCancelApp.Visible = false;
                tbnCheck.Visible = false;
                tbnUnChek.Visible = false;
                return;
            }
            if (Request.QueryString["Type"] != null)
            {
                tbEType.Text = Request.QueryString["Type"].ToString();
            }
            strID = Request.QueryString["id"].ToString();
            if (!Page.IsPostBack)
            {
                tbEType.Style.Add("display", "none");
                tbID.Style.Add("display", "none");
                LabMS.Model.RecipientsApp mRecipientsApp = checkQueryString(strID);
                if (mRecipientsApp == null)
                {
                    Common.JShelper.JSAlert(Page, "err2", "借出申请不存在!");
                    btnApp.Visible = false;
                    btnDel.Visible = false;
                    btnEdit.Visible = false;
                    btnCancelApp.Visible = false;
                    tbnCheck.Visible = false;
                    tbnUnChek.Visible = false;
                    return;
                }

                bind(mRecipientsApp);
                iniBtn(mRecipientsApp.State);
                bindDDL(mRecipientsApp);
                tbID.Text = mRecipientsApp.ID.ToString();

            }

        }

        void iniBtn(string strStatus)
        {
            btnApp.Visible = false;
            btnDel.Visible = false;
            btnEdit.Visible = false;
            btnCancelApp.Visible = false;
            tbnCheck.Visible = false;
            tbnUnChek.Visible = false;

            if (strStatus == "0")
            {
                btnApp.Visible = true;
                btnDel.Visible = true;
                btnEdit.Visible = true;
            }
            if (strStatus == "1")
            {
                btnCancelApp.Visible = true;
                tbnCheck.Visible = true;
                tbnUnChek.Visible = true;
                paneltoCheck.Visible = true;
            }
            if (strStatus == "2")
            {
                btnEdit.Visible = true;
                btnDel.Visible = true;
                panelExamine.Visible = true;
            }
            if (strStatus == "3")
            {
                btnDel.Visible = true;
                panelExamine.Visible = true;
            }
        }

        LabMS.Model.RecipientsApp checkQueryString(string strID)
        {
            LabMS.BLL.RecipientsApp bRecipientsApp = new LabMS.BLL.RecipientsApp();
            string strWhere = "ID =" + strID;
            List<LabMS.Model.RecipientsApp> listmRecipientsApp = bRecipientsApp.GetModelList(strWhere);
            if (listmRecipientsApp.Count == 0)
            {
                return null;
            }
            return listmRecipientsApp[0];
        }

        public void bindDDL(LabMS.Model.RecipientsApp mRecipientsApp)
        {
            LabMS.BLL.Lab blab = new LabMS.BLL.Lab();
            List<LabMS.Model.Lab> mLab = new List<LabMS.Model.Lab>();
            mLab = blab.GetModelList(" Lab_ParentID is null ");
            ddlLabs.DataSource = mLab;
            ddlLabs.DataTextField = "Lab_Name";
            ddlLabs.DataValueField = "Lab_Code";
            ddlLabs.DataBind();
            ddlLabs.SelectedValue = mRecipientsApp.EncodedWarehouse;

            ExtendBLL.Dictionary eDictionary = new ExtendBLL.Dictionary();
            List<Model.Dictionary> listCClass = new List<LabMS.Model.Dictionary>();

            listCClass = eDictionary.GetItemListByDClass("shiyongfangxiang");
            ddlArrow.DataSource = listCClass;
            ddlArrow.DataTextField = "Name";
            ddlArrow.DataValueField = "Code";
            ddlArrow.DataBind();
            ddlArrow.SelectedValue = mRecipientsApp.Arrow;

            ddlEType.SelectedValue = mRecipientsApp.Type;

        }

        void bind(LabMS.Model.RecipientsApp recipientsApp)
        {
          
            lbState.Text = Common.Status.GetOpenProjectStatus(recipientsApp.State);
            Comment.Text = Common.codehelper.Decode(recipientsApp.AppComment);

            tbUserNub.Text = recipientsApp.AppNub;
            tbCNub.Text = recipientsApp.ClassNumber;


            LabMS.BLL.Lab blab = new LabMS.BLL.Lab();
            List<LabMS.Model.Lab> mLab = new List<LabMS.Model.Lab>();
            mLab = blab.GetModelList("Lab_Code='"+ recipientsApp.Laboratory +"'");
            if(mLab.Count>0)
            {
                tbLabName.Text = mLab[0].Lab_Name;
            }

            lbMPerson.Text = recipientsApp.MRecipients;

            lbCName.Text = recipientsApp.Name;
            try
            {
                tbRecipientsDate.Text = recipientsApp.RecipientTime.Value.ToShortDateString();
            }
            catch { }
          
           
            lbTypeNub.Text = recipientsApp.Words;
            VerifyPerson.Text = recipientsApp.VerifyPerson;
            try
            {
                VerifyTime.Text = recipientsApp.VerifyTime.Value.ToShortDateString();
            }
            catch { }
            lbVerifyComment.Text = Common.codehelper.Decode(recipientsApp.VerifyComment);
            
        }

        protected void btnApp_Click(object sender, EventArgs e)
        {
            lbErr.Visible = false;
            Button btn = (Button)sender;
            string strAudit = "";
            string strAlert = "";
            if (btn.ID == "btnCancelApp")
            {
                strAudit = "0";
                strAlert = "撤销申请成功!";
            }
            if (btn.ID == "btnApp")
            {
                strAudit = "1";
                strAlert = "申请成功,请等待审核!";
            }
            if (btn.ID == "tbnCheck")
            {
                strAudit = "3";
                strAlert = "审核成功!";

                LabMS.BLL.RecipientsApp brapp = new LabMS.BLL.RecipientsApp();
                LabMS.Model.RecipientsApp mrecipeintsapp = new LabMS.Model.RecipientsApp();
                try
                {
                    mrecipeintsapp = brapp.GetModel(long.Parse(tbID.Text));
                }
                catch (Exception ex)
                {
                    lbErr.Text = ex.Message;
                    lbErr.Visible = true;
                    return;
                }
                if (mrecipeintsapp == null)
                {
                    Common.JShelper.JSAlert(Page, "err","数据错误，请删除此条记录!");
                    return;
                }
                List<LabMS.Model.InstrumentAndConsumables> mconsum = new List<LabMS.Model.InstrumentAndConsumables>();
                LabMS.BLL.InstrumentAndConsumables bconsum = new LabMS.BLL.InstrumentAndConsumables();

                mconsum = bconsum.GetModelList("Numbers='" + mrecipeintsapp.ClassNumber + "'");
                if (mconsum.Count == 0)
                {
                    Common.JShelper.JSAlert(Page, "err", "数据错误，请删除此条记录!");
                    return;
                }
                decimal iQuantityRecipients = 0;
                decimal.TryParse(mrecipeintsapp.AppNub, out iQuantityRecipients);
                if (iQuantityRecipients > mconsum[0].Quantity)
                {
                    Common.JShelper.JSAlert(Page, "err", "库存不足!");
                    return;
                }
                else
                {
                    if (mrecipeintsapp.Type == "xiaohaopin")
                    {
                        LabMS.Model.SuppliesRecipients model = new LabMS.Model.SuppliesRecipients();
                        model.Arrow = mrecipeintsapp.Arrow;
                        model.Comment = "通过申请借出!";
                        model.EncodedTime = mrecipeintsapp.RecipientTime;
                        model.EncodedWarehouse = mrecipeintsapp.EncodedWarehouse;
                        model.Laboratory = mrecipeintsapp.Laboratory;
                        model.Name = mrecipeintsapp.Name;
                        model.Numbers = mrecipeintsapp.ClassNumber;
                        model.OperatDate = DateTime.Today;
                        model.Operator = "sys";//用户
                        model.QuantityRecipients = mrecipeintsapp.AppNub;
                        model.Recipients = mrecipeintsapp.MRecipients;
                        model.Words = mrecipeintsapp.Words;

                        ExtendBLL.SuppliesRecipients EbSRecipient = new ExtendBLL.SuppliesRecipients();
                        try
                        {
                            EbSRecipient.ExtendAdd(model, (decimal)mconsum[0].Quantity);
                        }
                        catch (Exception ex)
                        {
                            lbErr.Visible = true;
                            lbErr.Text = ex.Message.ToString();
                            return;
                        }
                    }
                    else
                    {
                        ExtendBLL.Recipients brecipients = new ExtendBLL.Recipients();
                        Model.Recipients recipients = new LabMS.Model.Recipients();
                        recipients.BorrowedState = "良好";
                        recipients.Comment = "通过申请借出!";
                        recipients.EncodedTime = mrecipeintsapp.RecipientTime;
                        recipients.MRecipients = mrecipeintsapp.MRecipients;
                        recipients.Name = mrecipeintsapp.Name;
                        recipients.Numbers = mrecipeintsapp.ClassNumber;
                        recipients.OperatDate = DateTime.Today;
                        recipients.Operator = "";
                        recipients.Type = mrecipeintsapp.Type;

                        try
                        {
                            brecipients.ExtendAdd(recipients);
                        }
                        catch (Exception ex)
                        {
                            lbErr.Visible = true;
                            lbErr.Text = ex.Message.ToString();
                            return;
                        }
                    }
                }
            }
            if (btn.ID == "tbnUnChek")
            {
                strAudit = "2";
                strAlert = "审核成功!";
            }
            ExtendBLL.RecipientsApp extendrapp = new ExtendBLL.RecipientsApp();
            string strID = tbID.Text;
            try
            {
                if (btn.ID == "btnApp" || btn.ID == "btnCancelApp")
                {
                    extendrapp.UpdateStatus(strAudit, strID);
                }
                else
                {
                    string strChecker = UserRealName;//用户名
                    string strCheckContent = VerifyComment.Text.Trim();
                    extendrapp.SetCheck(strAudit, strID, DateTime.Today.ToShortDateString(), strChecker, strCheckContent);
                }
            }
            catch (Exception ex)
            {
                lbErr.Text = ex.Message;
                lbErr.Visible = true;
                return;
            }
            if (tbEType.Text != "")
            {
                Common.JShelper.JSAlertAndRedirect(Page, "success", strAlert, "recipientappperview.aspx?ID=" + tbID.Text + "&Type=" + tbEType.Text);
            }
            else
            {
                Common.JShelper.JSAlertAndRedirect(Page, "success", strAlert, "recipientappperview.aspx?ID=" + tbID.Text);
            }
        }

        protected void Del_Click(object sender, EventArgs e)
        {
            lbErr.Visible = false;
            LabMS.BLL.RecipientsApp brapp = new LabMS.BLL.RecipientsApp();
            string strID = tbID.Text;
            string strAlert = "删除成功!";
            try
            {
                brapp.Delete(long.Parse(strID));
            }
            catch (Exception ex)
            {
                lbErr.Text = ex.Message;
                lbErr.Visible = true;
                return;
            }
            if (tbEType.Text != "")
            {
                Common.JShelper.JSAlertAndRedirect(Page, "success", strAlert, "recipientsapplist.aspx?Type=" + tbEType.Text);
            }
            else
            {
                Common.JShelper.JSAlertAndRedirect(Page, "success", strAlert, "recipientsapplist.aspx");
            }
            
        }
    }
}
